package offer.middle;

import java.util.Arrays;

public class NumbersAppearOnce {

    public int[] findOnceNumbers(int[] array) {
        int xor = array[0];
        int[] result = new int[2];

        for (int i = 1; i < array.length; i++) {
            xor ^= array[i];
        }

        xor -= xor&(xor - 1);
        for (int i = 0; i < array.length; i++) {
            if ((xor&array[i]) == 0) {
                result[0] ^= array[i];
            } else {
                result[1] ^= array[i];
            }
        }

        if (result[0] > result[1]) {
            int temp = result[0];
            result[0] = result[1];
            result[1] = temp;
        }

        return result;
    }

    public static void main(String[] args) {
        int[] array = {1,2,1,6};
        NumbersAppearOnce once = new NumbersAppearOnce();
        System.out.println(Arrays.toString(once.findOnceNumbers(array)));
    }
}
